<?php
namespace app\index\controller;

use app\index\model\Vercode;
use think\Loader;
use \think\Request;
use app\index\model\User;

class Log extends Common
{
    public function login()
    {
        $model = new Vercode();
        $question = $model->getRand();
        return $this->fetch('login',[
            'title'=>'登陆',
            'question'=>$question
        ]);
    }

    public function reg()
    {
        $model = new Vercode();
        $question = $model->getRand();
        return $this->fetch('reg',[
            'title'=>'注册',
            'question' => $question
        ]);
    }
    public function regCheck()
    {
        $data = Request::instance()->param();
        $model = new Vercode();
        $model ->checkCode($data['vercode']);
        $validate = Loader::validate('User');
        if(!$validate->check($data)){
            exit(json_encode(['error'=>1,'info'=>$validate->getError()]));
        }
        $user = new User();
        $user->addUser($data);

    }
    public function logCheck()
    {
        $data = Request::instance()->param();
        $model = new Vercode();
        $model ->checkCode($data['vercode']);
        $validate = Loader::validate('User');
        if(!$validate->scene('login')->check($data)){
            exit(json_encode(['error'=>1,'info'=>$validate->getError()]));
        }
        $user = new User();
        $user->checkUser($data);
    }
    public function logout()
    {
        session('nickname', null);
        session('face', null);
        session('uid', null);
        $this->redirect(url('index/log/login'));

    }
    public function qqlog()
    {
        require_once("./think/vendor/qqlog/API/qqConnectAPI.php");
        $qc = new \QC();
        $url = $qc->qq_login();
        $this->redirect($url);
    }
    public function checkqqlog()
    {
        require_once("./think/vendor/qqlog/API/qqConnectAPI.php");
        $qc = new \QC();
        $qc->qq_callback();
        $openid = $qc->get_openid();
        $res = db('user')->where('openid',$openid)->find();
        if ($res){

            session('uid',$res['uid']);
            session('face',$res['face']);
            session('nickname',$res['nickname']);
            $this->redirect(url('index/index/index'));

        }else{
            require_once("./think/vendor/qqlog/API/qqConnectAPI.php");
            $qc = new \QC();
            $user = $qc->get_user_info();
            $res = db('user')->insert([
                'nickname'=>$user['nickname'],
                'sex'=>$user['gender'],
                'city'=>$user['city'],
                'face'=>$this->getface($user['figureurl_qq_2']),
                'openid'=>$openid,
                'ctime'=>time(),
            ]);
            if($res){
                $uid = db('user')->getLastInsID();
                session('uid',$uid);
                session('face',$res['face']);
                session('nickname',$res['nickname']);
                $this->redirect(url('index/index/index'));
            }else{
                $this->error('登录失败,请稍后再试',url('index/index/index'));
            }

        }


    }
    public function getface($url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        //打印获得的数据
        $name = uniqid();
        file_put_contents('./think/public/uploads/qqface/'.$name.'.png',$output);
        return '/uploads/qqface/'.$name.'.png';
    }
    public function checkEmail()
    {
        require '/think/vendor/phpmail/PHPMailerAutoload.php';

        $mail = new \PHPMailer(true);
        try {
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.qq.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'lishiwei3010@foxmail.com';                 // SMTP username
            $mail->Password = 'mlhmdqxhlfptbajj';                           // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to
            $mail->setFrom('lishiwei3010@foxmail.com', '灌水论坛');
            $mail->addAddress('1318685713@qq.com');     // Add a recipient
            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = 'Here is the subject';
            $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            $mail->send();
            echo 'Message has been sent';
        } catch (Exception $e) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        }
    }

}